package com.chhuang.system.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chhuang.core.bean.QueryBean;
import com.chhuang.core.service.impl.BaseServiceImpl;
import com.chhuang.system.po.ChUser;
import com.chhuang.system.po.ChUserExample;
import com.chhuang.system.bean.ChUserQueryBean;
import com.chhuang.system.mapper.ChUserMapper;
import com.chhuang.system.service.IChUserService;
import com.chhuang.utils.Assert;

/**
* ChUser ServiceImpl
* @author ChHuang
* @date Fri Mar 03 13:20:59 CST 2017
*/
@Service
public class ChUserServiceImpl extends BaseServiceImpl<ChUserMapper, ChUser> implements IChUserService {
	@Autowired
	private ChUserMapper chUserMapper;

	@Override
	public QueryBean<ChUser> seniorQuery(QueryBean<ChUser> queryBean) throws Exception {
		ChUserQueryBean poQueryBean = (ChUserQueryBean)queryBean;
		ChUserExample example = new ChUserExample();
		
		if(poQueryBean.isAnd()){//全部是与条件
			ChUserExample.Criteria criteria = example.createCriteria();
			if(Assert.isValidString(poQueryBean.getUsername())){
				criteria.andUsernameLike("%"+poQueryBean.getUsername()+"%");
			}
			if(Assert.isValidString(poQueryBean.getNickname())){
				criteria.andNicknameLike("%"+poQueryBean.getNickname()+"%");
			}
			if(Assert.isValidString(poQueryBean.getEmail())){
				criteria.andEmailLike("%"+poQueryBean.getEmail()+"%");
			}
			if(Assert.isValidString(poQueryBean.getPhone())){
				criteria.andPhoneLike("%"+poQueryBean.getPhone()+"%");
			}
		}else{//全部是或条件
			if(Assert.isValidString(poQueryBean.getUsername())){
				example.or().andUsernameLike("%"+poQueryBean.getUsername()+"%");
			}
			if(Assert.isValidString(poQueryBean.getNickname())){
				example.or().andNicknameLike("%"+poQueryBean.getNickname()+"%");
			}
			if(Assert.isValidString(poQueryBean.getEmail())){
				example.or().andEmailLike("%"+poQueryBean.getEmail()+"%");
			}
			if(Assert.isValidString(poQueryBean.getPhone())){
				example.or().andPhoneLike("%"+poQueryBean.getPhone()+"%");
			}
		}
		
		poQueryBean.setItemCount(chUserMapper.countByExample(example));//总记录数
		if(poQueryBean.getItemCount()>0){//计算总页数
			poQueryBean.setPageCount(poQueryBean.getItemCount()/poQueryBean.getPageSize()+
					(int)(poQueryBean.getItemCount()%poQueryBean.getPageSize()>0?1:0));
			if(poQueryBean.getPageCount()>=poQueryBean.getPageNum()){
				if(Assert.isValidString(poQueryBean.getOrderBy())){//排序
					example.setOrderByClause(poQueryBean.getOrderBy()+" "+poQueryBean.getOrder());
				}
				//分页
				example.setOffset((poQueryBean.getPageNum()-1)*poQueryBean.getPageSize());
				example.setLimit(poQueryBean.getPageSize());
				poQueryBean.setResult(chUserMapper.selectByExample(example));
			}
		}
		
		return poQueryBean;
	}
	
	@Override
	public List<ChUser> findByUsername(String username) throws Exception {
		ChUserExample example = new ChUserExample();
		example.createCriteria().andUsernameEqualTo(username);
		return chUserMapper.selectByExample(example);
	}

	@Override
	public int deleteByUserIdArray(String[] userIds) throws Exception {
		ChUserExample example = new ChUserExample();
		example.createCriteria().andUserIdIn(Arrays.asList(userIds));
		return chUserMapper.deleteByExample(example);
	}
}